package com.company.ems.controller;

import com.company.ems.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/dashboard")
public class DashboardServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession(false);
        User currentUser = (session != null) ? (User) session.getAttribute("user") : null;

        if (currentUser == null) {
            // 未登录，跳转到登录页
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return;
        }

        // 已登录，设置用户信息供 JSP 显示
        request.setAttribute("user", currentUser);
        request.getRequestDispatcher("/dashboard.jsp").forward(request, response);
    }
}
